package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;

import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysDynamic;
import com.ruoyi.project.system.domain.SysDynamicFile;
import com.ruoyi.project.system.domain.SysFollow;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Api("用户关注")
@RequestMapping("/mobileUser/follow")
public class UserFollowController extends BaseController {
    @Autowired
    private ISysFollowService sysFollowService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;
    @Autowired
    private ISysDynamicService sysDynamicService;
    @Autowired
    private ISysDynamicFileService sysDynamicFileService;
    @Autowired
    private ISysDynamicPariseService sysDynamicPariseService;


    @ApiOperation("获取关注列表")
    @GetMapping(value = "/list")
    public AjaxResult getInfo(SysFollow sysFollow) {
        startPage();
        sysFollow.setFensiUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysFollow> sysFollowList = sysFollowService.selectSysFollowList(sysFollow);
        for (int i = 0; i < sysFollowList.size(); i++) {
            Long benRenId = sysFollowList.get(i).getFensiUserId();
            Long bieRenId = sysFollowList.get(i).getUserId();
            SysMobileUser benRen = sysMobileUserService.selectSysMobileUserById(benRenId);
            SysMobileUser bieRen = sysMobileUserService.selectSysMobileUserById(bieRenId);
            sysFollowList.get(i).setBenRen(benRen);
            sysFollowList.get(i).setBieRen(bieRen);
        }
        return AjaxResult.success(sysFollowList);
    }


    @ApiOperation("新增关注")
    @Log(title = "新增关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFollow sysFollow) {
        sysFollow.setFensiUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        sysFollow.setDelFlag("0");
        List<SysFollow> sysFollowList = sysFollowService.selectSysFollowList(sysFollow);
        if (sysFollowList.size() != 0) {
            return AjaxResult.error("您已关注此人！");
        }
        sysFollow.setDelFlag("0");
        return toAjax(sysFollowService.insertSysFollow(sysFollow));
    }


    @ApiOperation("删除关注")
    @Log(title = "删除关注", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{followIds}")
    public AjaxResult remove(@PathVariable Long[] followIds) {
        return toAjax(sysFollowService.deleteSysFollowByIds(followIds));
    }

    @ApiOperation("删除关注")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysFollow sysFollow) {
        sysFollow.setFensiUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysFollow> sysFollowList = sysFollowService.selectSysFollowList(sysFollow);
        SysFollow sysFollow1 = sysFollowList.get(0);
        return toAjax(sysFollowService.deleteSysFollowById(sysFollow1.getFollowId()));
    }


    @ApiOperation("获取关注列表好友动态")
    @GetMapping(value = "/getFensDynamic")
    public AjaxResult getFensDynamic(SysFollow sysFollow) {
        startPage();
        sysFollow.setFensiUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysFollow> sysFollowList = sysFollowService.selectSysFollowList(sysFollow);
        List<SysDynamic> dynamicList = new ArrayList<>();
        for (int i = 0; i < sysFollowList.size(); i++) {
            Long bieRenId = sysFollowList.get(i).getUserId();
            SysDynamic bieRenDynamic = new SysDynamic();
            bieRenDynamic.setMobileUserId(bieRenId);
            List<SysDynamic> bieRenDynamicList = sysDynamicService.selectSysDynamicList(bieRenDynamic);
            for (int i1 = 0; i1 < bieRenDynamicList.size(); i1++) {
                Long dynamicId = bieRenDynamicList.get(i1).getDynamicId();
                List<SysDynamicFile> sysDynamicFiles = sysDynamicFileService.selectSysDynamicFileByDynamicId(dynamicId);
                bieRenDynamicList.get(i1).setDynamicFileList(sysDynamicFiles);
                Long mobileUserId = bieRenDynamicList.get(i1).getMobileUserId();
                SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
                bieRenDynamicList.get(i1).setSysMobileUser(sysMobileUser);
                dynamicList.add(bieRenDynamicList.get(i1));
            }
            sysFollowList.get(i).setDynamicList(bieRenDynamicList);
        }
        //本人动态
        SysDynamic benRenDynamic = new SysDynamic();
        benRenDynamic.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysDynamic> benRenDynamicList = sysDynamicService.selectSysDynamicList(benRenDynamic);
        for (int i1 = 0; i1 < benRenDynamicList.size(); i1++) {
            Long dynamicId = benRenDynamicList.get(i1).getDynamicId();
            List<SysDynamicFile> sysDynamicFiles = sysDynamicFileService.selectSysDynamicFileByDynamicId(dynamicId);
            benRenDynamicList.get(i1).setDynamicFileList(sysDynamicFiles);
            Long mobileUserId = benRenDynamicList.get(i1).getMobileUserId();
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
            benRenDynamicList.get(i1).setSysMobileUser(sysMobileUser);
            dynamicList.add(benRenDynamicList.get(i1));
        }
        Collections.sort(dynamicList, new Comparator<SysDynamic>() {
            @Override
            public int compare(SysDynamic o1, SysDynamic o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return AjaxResult.success(dynamicList);
    }

    @ApiOperation("获取自己关注的人")
    @GetMapping(value = "/getMyFans")
    public TableDataInfo getMyFans(SysFollow sysFollow) {
        startPage();
        sysFollow.setFensiUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysFollow> sysFollowList = sysFollowService.selectSysFollowList(sysFollow);
        for (int i = 0; i < sysFollowList.size(); i++) {
            Long bieRenId = sysFollowList.get(i).getUserId();
            SysMobileUser bieRen = sysMobileUserService.selectSysMobileUserById(bieRenId);

            sysFollowList.get(i).setBieRen(bieRen);
        }
        return getDataTable(sysFollowList);
    }

    @ApiOperation("获取自己粉丝")
    @GetMapping(value = "/getMyFollow")
    public TableDataInfo getMyFollow(SysFollow sysFollow) {
        startPage();
        sysFollow.setUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysFollow> sysFollowList = sysFollowService.selectSysFollowList(sysFollow);
        for (int i = 0; i < sysFollowList.size(); i++) {
            Long bieRenId = sysFollowList.get(i).getFensiUserId();
            SysMobileUser bieRen = sysMobileUserService.selectSysMobileUserById(bieRenId);
            sysFollowList.get(i).setBieRen(bieRen);
        }
        return getDataTable(sysFollowList);
    }
}
